package com.hjx.http.cookie;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieIdentityComparator;

/**
 * 可修改的CookieStore 因为用到了threadLocal 所以无需考虑线程安全问题，每个线程的cookieStore都是独一无二的
 *
 * Created by Administrator on 2017/6/14.
 */
public class ModifiableCookieStore implements CookieStore {

    private static final long serialVersionUID = -7581093305228232025L;

    private static final ThreadLocal<ModifiableCookieStore> cookieThreadLocal = new ThreadLocal<ModifiableCookieStore>();
    private TreeSet<Cookie> cookies;
    private TreeSet<Cookie> cookies2;

    public ModifiableCookieStore() {
        super();
        cookies = new TreeSet<Cookie>(new CookieIdentityComparator());
        cookies2 = new TreeSet<Cookie>(new CookieIdentityComparator());
    }

    public ModifiableCookieStore createOrGetCookieStore(){
        if (cookieThreadLocal.get() == null) {
            cookieThreadLocal.set(new ModifiableCookieStore());
        }
        return cookieThreadLocal.get();
    }

    @Override
    public void addCookie(final Cookie cookie) {
        if (cookie != null) {
            // first remove any old cookie that is equivalent
            createOrGetCookieStore().cookies.remove(cookie);
            if (!cookie.isExpired(new Date())) {
                createOrGetCookieStore().cookies.add(cookie);
            }
        }
    }

    public void addCookies(final Cookie[] cookies) {
        if (cookies != null) {
            for (final Cookie cooky : cookies) {
                createOrGetCookieStore().addCookie(cooky);
            }
        }
    }

    public Cookie getCookie(String name) {
        for (Cookie cookie: getCookies()) {
            if (cookie.getName().equals(name)) {
                return cookie;
            }
        }
        return null;
    }

    @Override
    public List<Cookie> getCookies() {
        //create defensive copy so it won't be concurrently modified
        return new ArrayList<Cookie>(createOrGetCookieStore().cookies);
    }

    public void remove(String name) {
        //create defensive copy so it won't be concurrently modified

        for (Cookie cookie : createOrGetCookieStore().cookies) {
            if (!cookie.getName().equals(name)) {
                createOrGetCookieStore().cookies2.add(cookie);
            }
        }
        createOrGetCookieStore().cookies.clear();
        createOrGetCookieStore().cookies.addAll(createOrGetCookieStore().cookies2);
        createOrGetCookieStore().cookies2.clear();
    }

    @Override
    public boolean clearExpired(final Date date) {
        if (date == null) {
            return false;
        }
        boolean removed = false;
        for (final Iterator<Cookie> it = createOrGetCookieStore().cookies.iterator(); it.hasNext();) {
            if (it.next().isExpired(date)) {
                it.remove();
                removed = true;
            }
        }
        return removed;
    }

    /**
     * Clears all cookies.
     */
    @Override
    public void clear() {
        createOrGetCookieStore().cookies.clear();
        cookieThreadLocal.remove();
    }

    @Override
    public String toString() {
        return createOrGetCookieStore().cookies.toString();
    }

}
